package com.it.jzs.controller.admin;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.UUID;
import com.it.jzs.pojo.Tag;
import com.it.jzs.pojo.Type;
import com.it.jzs.service.admin.AdminTagService;
import com.it.jzs.service.admin.AdminTypeService;
import com.it.jzs.vo.Result;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("admin")
public class AdminTagController {
    @Resource
    private AdminTagService adminTagService;

    @GetMapping("tag")
    public Result<Object> ListTypePage(@RequestParam("index") Integer index, @RequestParam("size") Integer size,@RequestParam("keyWords") String keyWords) {  // 分页查询
//        System.out.println("page = " + index);
//        System.out.println("size = " + size);
        Result<Object> result = adminTagService.selectPage(index, size,keyWords);
//        System.out.println("result = " + result);
        return result;
    }

    @PostMapping("tag")
    public Result<Type> addType(@RequestBody Tag tag) {  // 新增
//        System.out.println("type = " + type);
        System.out.println("tag = " + tag);
        adminTagService.insertOne(tag);
        return Result.success(null, "success");
    }

    @DeleteMapping("tag/{id}")
    public Result<Object> deleteType(@PathVariable("id") Integer id) {  // 删除分类
//        System.out.println("id = " + id);
        Result<Object> result = adminTagService.delete(id);
        return result;
    }

    @PutMapping("tag")
    public Result<Object> updateType(@RequestBody Tag tag) {
//        System.out.println("type = " + type);
        Result<Object> result = adminTagService.update(tag);
        return result;
    }

    @PostMapping("/tag/upload")
    public Result<String> upload(MultipartFile file) throws IOException { // 分类图片上传接口

        String originalFilename = file.getOriginalFilename();// 获取上传的文件名
        int i = originalFilename.lastIndexOf("."); // 查看最后一个出现.的位置
        String houzui = originalFilename.substring(i);  // 截取 这个索引开始也就是.开始的后缀
        UUID uuid = UUID.randomUUID();
//        System.out.println("uuid.toString() = " + uuid.toString());
        StringBuilder builder = new StringBuilder();
        builder.append(uuid).append(houzui);  // 减少因为字符串的拼接消耗的性能
        String rootFilePath = "E:\\upload01\\" + builder.toString();
//        System.out.println("originalFilename = " + originalFilename);
        File file1 = FileUtil.writeBytes(file.getBytes(), rootFilePath);
//        System.out.println("file = " + file);
        return Result.success(new String(builder.toString()), "上传成功");
    }

    @GetMapping("tag/all")
    public Result<Object> selectAllTag() {
//        System.out.println("type = " + type);
        Result<Object> result = adminTagService.findAll();
        return result;
    }


    @GetMapping("tag/addblog")   // 这个接口用来专门获取 新增博客的时候所需要的的回显数据
    public Result<Object> selectAddBlogVo() {
        Result<Object> result = adminTagService.findAddBlogTagVo();
        return result;
    }

    @PostMapping("tag/deletebatch")
    public Result<Object> deleteBatch(@RequestBody List<Integer> ids) {  // 批量删除
        Result<Object> result = adminTagService.deleteBatch(ids);

        return result;
    }
}
